package com.leyou.sh.auth.web;

import com.leyou.sh.auth.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author balisong_fcs
 * @date 2021/1/11 22:07
 * @desc
 */
@RestController
@RequestMapping("/user")
public class UserAuthController {
    @Autowired
    private UserAuthService userAuthService;

    /**
     * 处理用户的登录业务,无返回结果,jwt生成的token将生成cookie中写入响应头写回
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void> login(
            @RequestParam("username")String username,
            @RequestParam("password")String password,
            HttpServletResponse response
    ){
        this.userAuthService.login(username, password, response);
        return ResponseEntity.ok().build();
    }
    @PostMapping("/logout")
    public ResponseEntity<Void> logout(HttpServletRequest request,HttpServletResponse response){
        //删除redis中的用户对应的jti
        //删除cookie,覆盖
        this.userAuthService.logout(request, response);
        return ResponseEntity.ok().build();
    }
}
